<?php
/**
 * 区域  
 * AreaController.php
 * ==============================================
 * 版权所有 2013-2014
 * ----------------------------------------------
 * 这不是一个自由软件，未经授权不许任何使用和传播。
 * -------------------------------
 * 
 * @package xcs
 * @author huangdaohai <huangdaohai.2007@163.com>
 * @version V1.0
 * @access public
 */
class AreaController extends AdminController 
{
	protected $oModel;	
	public function __construct($id, $module) {
		$this->setPageTitle('区域信息');
		$this->oModel = new AdminAreaHandle();
		return parent::__construct($id, $module);
	}
	
	public function actionIndex() {
	    
		$params['page_size'] = $this->pagesize;
		$params['page'] = $this->_request->getQuery('page' ,0);
		$params['name'] = $this->_request->getQuery('name');
		$result = $this->oModel->getList($params);
        $result['params'] = $params;
		$this->setPageTitle(array('区域信息','列表'));	
		$this->render('index', $result);
	}
	/**
	 * AreaController::actionGetChildren() 获取区域子级
	 * 
	 * @return void
	 */
	public function actionGetChildren() {
		$fidstr = $this->_request->getQuery('fidstr');
		$fidstr = json_decode($fidstr);
		$childrenstr = $this->oModel->getTreeTable($fidstr);
		echo $childrenstr;
	}	
	/**
	 * AreaController::actionCache() 更新JS缓存
	 * 
	 * @return void
	 */
	public function actionCache() {
		if(BaseAreaModel::model()->createJSFile()) {
			$this->redirect($this->createUrl('area/index'),'缓存更新成功');
		} else {
			$this->redirect($this->createUrl('area/indez'),'缓存更新失败');
		}
	}	
	/**
	 * AreaController::actionUpdate() 添加更新区域信息
	 * 
	 * @return void
	 */
	public function actionUpdate() {
	    $iId = $this->_request->getQuery('id' ,0);
        $oModel = $iId ? BaseAreaModel::model()->findByPk($iId) : new BaseAreaModel();
        $aList = BaseAreaModel::model()->listing();
		if(isset($_POST['ajax']) && $_POST['ajax']==='area-_form-form')
		{
			echo CActiveForm::validate($oModel);
			Yii::app()->end();
		}
        $aPost = $this->_request->getPost('BaseAreaModel');
        if( ! empty( $aPost ) ){
            //print_r($aPost);exit;
            if( BaseAreaModel::model()->change($aPost) ){
                $alert="SendWebMsg('success','操作成功!','".$this->createUrl('area/index')."')";
            } else {
                $alert="SendWebMsg('error','请修正页面错误提示项，操作失败!')";
            }
            Yii::app()->getClientScript()->registerScript('msg',$alert,CClientScript::POS_END);
        }
		$this->render('_form', array('model'=> $oModel ,'list' => $aList));
	}	
	/**
	 * AreaController::actionDelete() 删除数据
	 * 
	 * @return void
	 */
	public function actionDelete() {
		$iAreaId = $this->_request->getQuery('id' ,0);;
		
		if(BaseAreaModel::model()->remove($iAreaId)) {
			$this->ajaxReturn(0 ,'删除成功');
		} else {
			$this->redirect(1, '删除失败');
		}
	}
}
?>